<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>赋值运算符</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.arithmetic.html">算术运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">位运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.assignment" class="sect1">
   <h2 class="title">赋值运算符</h2>
   <p class="simpara">
    基本的赋值运算符是“=”。一开始可能会以为它是“等于”，其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。
   </p>
   <p class="para">
    赋值运算表达式的值也就是所赋的值。也就是说，“<em>$a = 3</em>”的值是 3。这样就可以做一些小技巧：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;现在成了&nbsp;9，而&nbsp;$b&nbsp;成了&nbsp;4。<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    对于数组 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>，对有名字的键赋值是用“=&gt;”运算符。此运算符的<a href="language.operators.precedence.html" class="link">优先级</a>和其它赋值运算符相同。
   </p>
   <p class="para">
    在基本赋值运算符之外，还有适合于所有<a href="language.operators.html" class="link">二元算术</a>，数组集合和字符串运算符的“组合运算符”，这样可以在一个表达式中使用它的值并把表达式的结果赋给它，例如：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;sets&nbsp;$a&nbsp;to&nbsp;8,&nbsp;as&nbsp;if&nbsp;we&nbsp;had&nbsp;said:&nbsp;$a&nbsp;=&nbsp;$a&nbsp;+&nbsp;5;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"There!"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;sets&nbsp;$b&nbsp;to&nbsp;"Hello&nbsp;There!",&nbsp;just&nbsp;like&nbsp;$b&nbsp;=&nbsp;$b&nbsp;.&nbsp;"There!";<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    注意赋值运算将原变量的值拷贝到新变量中（传值赋值），所以改变其中一个并不影响另一个。这也适合于在密集循环中拷贝一些值例如大数组。
   </p>
   <p class="para">
    在 PHP 中普通的传值赋值行为有个例外就是碰到对象 <span class="type"><a href="language.types.object.html" class="type object">object</a></span> 时，在
	PHP 5 中是以引用赋值的，除非明确使用了 <a href="language.oop5.cloning.html" class="link">clone</a> 关键字来拷贝。
   </p>

   <div class="sect2" id="language.operators.assignment.reference">
    <h3 class="title">引用赋值</h3>
    <p class="para">
     PHP 支持引用赋值，使用“<span class="computeroutput">$var 
	 = &amp;$othervar;</span>”语法。引用赋值意味着两个变量指向了同一个数据，没有拷贝任何东西。
    </p>
    <p class="para">
     <div class="example" id="example-115">
      <p><strong>Example #1 引用赋值</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;是&nbsp;$a&nbsp;的引用<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;3<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;3<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;修改&nbsp;$a<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;输出&nbsp;4<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;也输出&nbsp;4，因为&nbsp;$b&nbsp;是&nbsp;$a&nbsp;的引用，因此也被改变<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     自 PHP 5 起，<a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a>
	 运算符自动返回一个引用，因此再对
	 <a href="language.oop5.basic.html#language.oop5.basic.new" class="link">new</a>
	 的结果进行引用赋值在 PHP 5.3 以及以后版本中会发出一条
     <strong><code>E_DEPRECATED</code></strong> 错误信息，在之前版本会发出一条
     <strong><code>E_STRICT</code></strong> 错误信息。
    </p>
    <p class="para">
     例如以下代码将产生警告：
     <div class="informalexample">
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{}<br /><br /></span><span style="color: #FF8000">/*&nbsp;The&nbsp;following&nbsp;line&nbsp;generates&nbsp;the&nbsp;following&nbsp;error&nbsp;message:<br />&nbsp;*&nbsp;Deprecated:&nbsp;Assigning&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;new&nbsp;by&nbsp;reference&nbsp;is&nbsp;deprecated&nbsp;in...<br />&nbsp;*/<br /></span><span style="color: #0000BB">$o&nbsp;</span><span style="color: #007700">=&nbsp;&amp;new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     有关引用更多信息参见本手册中<a href="language.references.html" class="link">引用的解释</a>一章。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.arithmetic.html">算术运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.bitwise.html">位运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
